<template>
  <el-dialog
    ref="wkDialog"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="$t('crm.components.80bcf5061593492d423adc4db2124fee')"
    width="400px"
    @close="handleCancel">
    <el-form
      ref="fieldForm"
      :model="form"
      :rules="rules"
      :validate-on-rule-change="false"
      label-position="top">
      <el-form-item
        class="wk-form-item"
        :label="$t('crm.components.af4079541e235828b0780453a803f262')"
        prop="status">
        <el-select v-model="form.status" style="width: 100%;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-debounce="handleConfirm" type="primary">
        {{ $t('crm.components.e8ebb043eca4dcfc37192de381b7fec6') }}
      </el-button>
      <el-button @click.native="handleCancel">
        {{ $t('crm.components.184e6446c36f4245ff7ef3140d53f429') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { crmCustomerDealStatusAPI } from '@/api/crm/customer'

import ElDialogLoadingMixin from '@/mixins/ElDialogLoading'

export default {
  /** 客户管理  成交状态 操作*/
  name: 'DealStatusHandle',
  components: {},
  mixins: [ElDialogLoadingMixin],
  props: {
    value: Number,
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    // 勾选数据
    selectionList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading: true,
      form: {
        status: 1
      },
      rules: {
        status: [{ required: true, message: this.$t('crm.components.05739e513fe76f59b65b69f41ca53628'), trigger: 'change' }]
      },
      options: [
        {
          label: this.$t('crm.components.35ef91530695edf82bad60771c14d2f6'),
          value: 1
        },
        {
          label: this.$t('crm.components.d2505eae15b3c5a8876f75c43999bf45'),
          value: 0
        }
      ]
    };
  },
  computed: {},
  watch: {
    value: {
      handler(val) {
        if (typeof val != 'undefined') {
          this.form = {
            status: val == 1 ? 1 : 0
          }
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    /**
     * 取消选择
     */
    handleCancel() {
      // 重置状态
      this.form = {
        status: 1
      }
      this.$emit('update:visible', false)
    },

    /**
     * 点击确定
     */
    handleConfirm() {
      this.$refs.fieldForm.validate((valid) => {
        if (valid) {
          const actionIds = this.selectionList.map(item => item.customerId).join(',')
          const params = {
            dealStatus: this.form.status
          }
          params.ids = actionIds
          this.loading = true
          crmCustomerDealStatusAPI(params)
            .then(res => {
              this.$message({
                type: 'success',
                message: this.$t('crm.components.8ee65ba776a0b0d02cc1abe3840047df')
              })
              this.loading = false
              this.$emit('handle', {
                type: 'deal_status'
              })
              this.handleCancel()
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/wk-form.scss";
</style>
